<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>

    <script>

        $(function(){
            $("#btn01").click(function(){
                $.ajax({
                    url:'${pageContext.request.contextPath}/ajax',//请求路径
                    type:'post',//请求的方法
                    data : {"name":"zhangsan","age":19},//请求数据,
                    dataType:'json',//返回值的类型
                    success: function(resData){   //{name:lisi,age:19}
                        alert(resData.name);
                        alert(resData.age);
                    }
                });
            });

            $("#btn02").click(function(){
                $.ajax({
                    url:'${pageContext.request.contextPath}/ajax1',//请求路径
                    type:'post',//请求的方法
                    data : JSON.stringify({"name":"zhangsan","age":19}),//请求数据,
                    contentType:'application/json;charset=UTF-8',//加了contentType之后，表示发送的时候，把data里面的数据，当成json发送给后端
                    dataType:'json',//返回值的类型
                    success: function(resData){   //{name:lisi,age:19}
                        alert(resData.name);
                        alert(resData.age);
                    }
                });
            });
        });
    </script>
</head>
<body>

<button id="btn01">发送普通请求</button>
<button id="btn02">发送json请求</button>

</body>
</html>
